package com.nintendo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nintendo.exceptions.GraceException;
import com.nintendo.mapper.UsersTSTMapper;
import com.nintendo.pojo.Users;
import com.nintendo.pojo.vo.NewFriendsVO;
import com.nintendo.result.ResponseStatusEnum;
import com.nintendo.service.UsersTSTService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

@Service
public class UsersTSTServiceImpl extends ServiceImpl<UsersTSTMapper, Users> implements UsersTSTService {
    @Autowired
    private UsersTSTMapper usersTSTMapper;
    @Override
    public Users searchUsersByMobile(String mobile, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        if(StringUtils.isBlank( mobile)){
            GraceException.display(ResponseStatusEnum.USER_MOBILE_NULL);
        }
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<Users>();
        queryWrapper.eq(Users::getMobile,mobile).or().eq(Users::getWechatNum, mobile).or()
                .eq(Users::getId, mobile);
        Users users = getOne(queryWrapper);
        if(Objects.isNull(users)){
            GraceException.display(ResponseStatusEnum.USER_NOT_EXIST);
        }

        return users;
    }

    @Override
    public List<NewFriendsVO> findMyRequest(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return usersTSTMapper.selectMyRequestByUserId(userId);
    }

    @Override
    public List<Users> searchMyFriend(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        List<Users> users = usersTSTMapper.selectMyFriend(userId);
        return users;
    }
}
